package com.lyshop.guiyingshanding.activitys;

import static com.lyshop.guiyingshanding.utils.RouteUtil.gotoActivity;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.adapters.TicketAdapter;
import com.lyshop.guiyingshanding.application.MyApplication;
import com.lyshop.guiyingshanding.base.BaseActivity;
import com.lyshop.guiyingshanding.base.BaseRVAdapter;
import com.lyshop.guiyingshanding.bean.TicketBean;
import com.lyshop.guiyingshanding.net.ActionCallbackListener;
import com.lyshop.guiyingshanding.utils.RouteUtil;
import com.lyshop.guiyingshanding.utils.ToastUtil;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.model.GoodsItemBean;

import java.util.ArrayList;
import java.util.List;

public class TicketActivity extends BaseActivity implements View.OnClickListener {

    private SmartRefreshLayout refresh_layout;
    private RecyclerView rv_ticket_record;
    private TicketAdapter adapter;
    private List list = new ArrayList<>();
    private TextView tv_right;
    private  int page = 1;
    private  TextView not_data_tv;
    @Override
    protected int setContentView() {
        return R.layout.activity_ticket_list;
    }

    @Override
    protected void findViews() {
        refresh_layout = $(R.id.refresh_layout);
        rv_ticket_record = $(R.id.rv_ticket_record);
        refresh_layout = $(R.id.refresh_layout);
        tv_right = $(R.id.tv_right) ;
        not_data_tv = $(R.id.not_data_tv);
        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        rv_ticket_record.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new TicketAdapter(mContext,list);
        rv_ticket_record.setAdapter(adapter);
        getListDate();
        adapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                TicketBean  ticketBean =  (TicketBean)data.get(position);
                String goods_id = ticketBean.getGoods_id().get(0);
                List<GoodsAllBean> allList = myApplication.getAllList();
                if (null != allList && allList.size() > 0) {
                    for (int i = 0; i < allList.size(); i++) {
                        GoodsAllBean  goodsbean =  allList.get(i);
                        for (int j = 0; j < goodsbean.goods_list.size(); j++) {
                            GoodsItemBean itemBean = (GoodsItemBean)goodsbean.getGoodsItemBean(j);
                            if (goods_id.equals(itemBean.goods_id)){
                                RouteUtil.toMarketActivity(TicketActivity.this, goodsbean.pro_code);
                                return;
                            }
                        }
                    }
                } else {
                    ToastUtil.showToast(mContext, "暂未获取商品信息");
                }
            }
        });
    }

    @Override
    protected void setListeners() {
        tv_right.setOnClickListener(this);
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getListDate();
            }
        });

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getListDate();
            }
        });
    }

    private  void getListDate(){
        refresh_layout.finishRefresh(2000);
        refresh_layout.finishLoadMore(2000);
        mAppAction =  MyApplication.getApplication().getAppAction();
        mAppAction.getUserTicketList("1",page, new ActionCallbackListener<List<TicketBean>>() {
            @Override
            public void onSuccess(List<TicketBean> data) {
                if (page == 1){
                    list.clear();
                }
                list.addAll(data);
                if(list.size()>0){
                    not_data_tv.setVisibility(View.GONE);
                    rv_ticket_record.setVisibility(View.VISIBLE);
                }else{
                    not_data_tv.setVisibility(View.VISIBLE);
                    rv_ticket_record.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.tv_right){
            gotoActivity(mContext, TicketHistoryActivity.class, null);
            return;
        }
    }
}
